﻿@page "/AuditSet/{id}"
@using AutoMapper;
@using Carlife.Inf;
@using CarlifeRespository;
@using Commonality;
@using ICarlifeRespository;
@using ICarlifeService;
@using Models;
@using static Commonality.Examineandapprove;
@inject NavigationManager navigationManager;

@inject IMinimumrentService MinimumrentService;
@inject IMapper mapper;
@inject IUOfWork UOfWork;
@inject ICarLeaseProjectService CarLeaseProjectService;
<h3 style="text-align:center">项目审核</h3>
<table class="table table-bordered" ShowLoadingInFirstRender="true" style="text-align:center">
    <tr>
        <td>审核意见</td>
        <td style="width:auto">
            <div class="col-12">
                <RadioList Items="@Ratio" @bind-Value="@BindingModel.Checked" />
            </div>
        </td>
    </tr>
    <tr>
        <td>审核理由</td>
        <td>
            <div class="col-12">
                <BootstrapInput PlaceHolder=" " @bind-Value="@BindingModels.Reasons" />
            </div>
        </td>
    </tr>
    <tr>
        <td>审核附件</td>
        <td>
            <InputFile OnChange="OnFileChange" />
        </td>
    </tr>
    <p>
        <label>
            上传信息：@Message
        </label>
    </p>
    
    <Button ButtonStyle="ButtonStyle.Round" Color="Color.Primary" OnClick="AddProjectApproval">保存</Button>
    <Button ButtonStyle="ButtonStyle.Round" Color="Color.Secondary">取消</Button>
    <p>
        <label>
            @results
        </label>
    </p>
</table>
@code {
    [Parameter]
    public  string id { get; set; }

    private IEnumerable<SelectedItem>? Ratio { get; set; }
    private AuditSetDTO BindingModel { get; set; } = new AuditSetDTO();
    private AuditSetsDTO BindingModels { get; set; } = new AuditSetsDTO();
    protected override async Task OnInitializedAsync()
    {
        Ratio = new List<SelectedItem>
        {
            new SelectedItem($"{Examine.Onlease}", "通过"),
            new SelectedItem($"{Examine.Project_approval_Confirm}", "拒绝"),
            new SelectedItem($"{Examine.Project_approval_Confirms}", "退回修改")
        };
    }
    private List<IBrowserFile> loadedFiles = new();
    private long maxFileSize = 1024 * 1024 * 24;
    private int maxAllowedFiles = 2;
    private bool isLoading;
    private string Message = string.Empty;
    private string results = string.Empty;
    private async Task OnFileChange(InputFileChangeEventArgs e)
    {

        // 这里可以添加处理上传文件的逻辑
        // 例如保存文件到服务器或者处理文件内容等
        isLoading = true;
        loadedFiles.Clear();
        foreach (var file in e.GetMultipleFiles(maxAllowedFiles))
        {
            try
            {
                loadedFiles.Add(file);

                string result = await FileHelpers.ProcessFormFile(file, maxFileSize);

                if (string.IsNullOrEmpty(result))
                {

                    Message = "上传失败！";
                }
                else

                    Message = "上传成功！";
            }
            catch (Exception ex)
            {
                Message = ex.Message;

            }
        }
        isLoading = false;

    }
    //保存
    public async Task<ResultReposeDTO> AddProjectApproval()
    {
        ResultReposeDTO result = new();
        using (var tarn = UOfWork.GetDbConnection().BeginTransaction())//开启事务
        {
            try
            {
                BindingModel.Id =Convert.ToInt32(id);
                await CarLeaseProjectService.Updates(mapper.Map<CarLeaseProject>(BindingModel));
                BindingModels.CustomerId =Convert.ToInt32(id);
                await MinimumrentService.Insert(mapper.Map<Minimumrent>(BindingModels));
                
                //成功提交
                tarn.Commit();
                if (result.code.Equals(ResponseResultCode.Success))
                {
                    results = "成功";
                    navigationManager.NavigateTo("/Upload");
                }
                else
                {
                    results = "失败";
                }
                return result;
                
            }
            catch (Exception)
            {
                //失败回滚
                tarn.Rollback();

                result.code = ResponseResultCode.Failed;
                return result;
            }
        }
       
    }
}
